package com.xd.sign.controller;

import com.xd.sign.dao.CommentRepository;
import com.xd.sign.entity.Sign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class PageController {
    @Autowired
    private CommentRepository commentRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    @RequestMapping("/index")
    public String index(){
        return "index";
    }

    @RequestMapping("/select")
    @ResponseBody
    public Map<String,Object> select(){
        Map<String,Object> map=new HashMap<>();
        //未签到
        List<Sign> allzero = commentRepository.findBySign(0);
        //签到
        List<Sign> allone = commentRepository.findBySign(1);
        map.put("Zero",allzero);
        map.put("One",allone);
        return map;
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map<String,Object> update(String name){
        Map<String,Object> map=new HashMap<>();
        Query query= Query.query(Criteria.where("name").is(name));
        Update update=new Update();
        update.set("sign",1);
        mongoTemplate.updateFirst(query,update,"sign");
        map.put("Code",1);
        return map;
    }
}
